package Ui;

import DBase.Coordinate;

/**
 * 
 * @author Elya & Dana
 */
public class GameUiConsole extends GameUi{
     
   
    /**
     * Get the number of players in the game
     * @return number of players
     */
    @Override    
    public int getPlayersNumber(){
        
        int numPlayers=0;
        String input;
        boolean isValid=false;

        System.out.println("\nPlease enter the Number of Players(number between 2-6):");

        input=s.nextLine();
        isValid=("2".equals(input) || "2".equals(input) || "3".equals(input) || "4".equals(input) ||"5".equals(input) || "6".equals(input));
        while(isValid==false){
            System.out.println("Oops!Wrong input.Please Try Again:");
            System.out.println("Please enter the Number of Players(number between 2-6):");

            input=s.nextLine();
            isValid=("2".equals(input) || "2".equals(input) || "3".equals(input) || "4".equals(input) ||"5".equals(input) || "6".equals(input));
        }
        numPlayers=input.charAt(0)-'0';//convert to int
        return numPlayers; 
    }
    
    /**
     * Get player names
     * @param numPlayers
     * @return string array
     */
    @Override
    public String [] getPlayersNames(int numPlayers){
        
        boolean flag=false;
        String name="";
        
        String [] PlayersNames = new String [numPlayers];
        for(int i = 0; i < numPlayers; i++){
            while(!flag){
                System.out.println("\nPlease enter player #" + (i+1) + "'s name:" ); 
                name=s.nextLine();
                //System.out.println(name);
                flag = checkIfNameValid(name,i,PlayersNames);
                if (flag==false)
                    System.out.println("The name is invalid.Might be a duplicate");
                    
            }
            PlayersNames[i]=name;
            flag=false;
        }
        return PlayersNames;
    }


    
    /**
     * Get ships names
     * @param shipSizesArray
     * @return ships names array
     */
    @Override
    public String [] getShipsData(int [] shipSizesArray){
        
        boolean flag=false;
        String name="";
        
        String [] shipsNames=new String[5];
        for(int i = 0; i < 5; i++){
            while(!flag){
                
                System.out.println("\nPlease enter Ship with size " + shipSizesArray[i] + "'s name:");
                name=s.nextLine();
                //System.out.println(name);
                flag = checkIfNameValid(name,i,shipsNames);
                if (flag==false)
                    System.out.println("The name is invalid.Might be a duplicate");
            }
            shipsNames[i]=name;
            flag=false;
        }
        return shipsNames;          
    }
    

    /**
     * Get players types array
     * @param numPlayers
     * @return integer array with either 1 or 2 (Human or Computer)
     */
    @Override
    public int [] getPlayersType(int numPlayers){
        
        String input="";
        char ch;
        
        int [] playerType = new int[numPlayers];
        System.out.println("\nPlease choose for each player if its human or compuer\n"); 

        for(int i = 0; i < numPlayers; i++){
            while (!( "1".equals(input) || "2".equals(input))){
            System.out.println("Player #" + (i+1) + ":press 1 for human, press 2 for computer."); 
            input=s.nextLine();
            if ("1".equals(input) || "2".equals(input)){
                ch=input.charAt(0);
                playerType[i] = ch-'0';
                }
            }
            input="";
        }
        return  playerType;
    }  
    
    /**
     * Place a ship on board
     * @param x row
     * @param y column
     * @param direction - Horizontal or Vertical
     * @param currentShipSize
     * @param PlayerName
     * @param name ship name
     */
    @Override
    public void PlaceShipsOnBoard(int [] x, int [] y,int [] direction,int currentShipSize,String PlayerName,String name){
            
        directionsOfPlacing(currentShipSize,PlayerName,name);     
        getCoordinateWithDirection(x,y,direction);     
    }       
        
    /**
     * Prints place ship with size
     * @param currentShipSize
     * @param PlayerName
     * @param shipName
     */
    public void directionsOfPlacing(int currentShipSize, String PlayerName,String shipName){  
        
        System.out.println("\nHi " + PlayerName + " please place ship "+shipName+"(with size "+currentShipSize+")");

          
    }
    
    /**
     * Gets via 3 parameters the coordinate with it's direction
     * @param x row
     * @param y column
     * @param direction Horizontal or Vertical
     */
    public void getCoordinateWithDirection(int[] x, int[] y, int []direction){
        

        String input;
        boolean isValid=false;

        System.out.println("Please enter the Direction of the ship:");
        System.out.println("For Horizontal press 1,for Vertical press 2.");

        input=s.nextLine();
        
        isValid=("1".equals(input) || "2".equals(input));
        
        while (isValid==false ){
            System.out.println("\nOops!Wrong input.Please Try Again:");
            System.out.println("For Horizontal press 1,for Vertical press 2.");

            input=s.nextLine();
            isValid=("1".equals(input) || "2".equals(input));
        }
        direction[0]=input.charAt(0)-'0';//convert to int
        
        //get the Coordinate inputs
        Coordinate coordinate=getCoordinateInput();
        x[0]=coordinate.getRow();
        y[0]=coordinate.getColumn();
        
    }
    
}
